<script type="text/javascript">
    $(document).ready(function() {
        $("#tabsdetails").tytabs({
            tabinit:"1",
            fadespeed:"fast"
        });
        
        $('.ca2').show();
    });
    
    function toggleSemaine(el) {
        var elId = $(el).attr('id');
        var wrc = $('.right-content').width()-38;
        
        //$('.pc-content').css('max-width',wrc);
        
        if ($(el).hasClass('plus')) {
            $(el).removeClass('plus');
            $(el).addClass('moins');            
            $('.'+elId).fadeIn(1000);
        } else if ($(el).hasClass('moins')) {
            $(el).removeClass('moins');
            $(el).addClass('plus');
            $('.'+elId).fadeOut(500);
        }
    }
</script>
<?php
//var_dump($period);

foreach ($period as $idxm => $month) { 
    $weekCount = 0;
    foreach ($month as $week) {
        $weekCount++;         
    }
} 
$width = number_format(100/($weekCount+1),2,',', ' ');
?>

<div id="tabsdetails">
    <ul class="tabs">
    <?php 
    $x = 1; 
    foreach ($period as $idxm => $month) { ?>
        <li id="tab<?php echo $x; ?>"><?php echo $idxm; ?></li>
    <?php $x++; } ?>
    </ul>
    
    <?php 
    $y = 1; 
    foreach ($period as $idxm => $month) { 
        // Get CA per month
        $monthCATTC = $monthCAHT = 0;
        foreach($ca as $dca) {
            $explodeOrderDate = explode('-',$dca['orderDate']);
            $thisMonth = $monthText[intval($explodeOrderDate[1])]." ".$explodeOrderDate[0];            
            
            if($thisMonth == $idxm) {
                $monthCATTC += $dca['ca_ttc'];
                $monthCAHT += $dca['ca_ht'];
            }
        }
        
        // Get Transaction per month
        $monthNbTran = 0;
        foreach($nbT as $nbTran) {
            $explodeOrderDate = explode('-',$nbTran['orderDate']);
            $thisMonth = $monthText[intval($explodeOrderDate[1])]." ".$explodeOrderDate[0];            
            
            if($thisMonth == $idxm) {
                $monthNbTran += $nbTran['nbTransaction'];
            }
        }
        
        // Get Nb Article per month
        $monthNbArt = 0;
        foreach($nbA as $nbArt) {
            $explodeOrderDate = explode('-',$nbArt['orderDate']);
            $thisMonth = $monthText[intval($explodeOrderDate[1])]." ".$explodeOrderDate[0];            
            
            if($thisMonth == $idxm) {
                $monthNbArt += $nbArt['nbArticle'];
            }
        }
        
        // Get Ecarts caisse per month
        $monthEc = 0;
        foreach($ecDays as $ecDay) {
            $explodeOrderDate = explode('-',$ecDay['date']);
            $thisMonth = $monthText[intval($explodeOrderDate[1])]." ".$explodeOrderDate[0];            
            
            if($thisMonth == $idxm) {
                $monthEc += $ecDay['ec'];
            }
        }
        
        // Get Repas per month
        $monthRepas = 0;
        foreach($repasDays as $repasDay) {
            $explodeOrderDate = explode('-',$repasDay['orderDate']);
            $thisMonth = $monthText[intval($explodeOrderDate[1])]." ".$explodeOrderDate[0];            
            
            if($thisMonth == $idxm) {
                $monthRepas += $repasDay['repas'];
            }
        }
    ?>
    <div class="contents">
        <div id="content<?php echo $y; ?>" class="tabscontent">
            <table>
                <tr>
                    <td style="width: 132px;"></td>
                    <td class="tabs-details-top">
                        <table>
                            <tr>
                                <th class="width175">
                                    <table class="width175">
                                        <tr class="haut2">
                                            <td colspan="2"><p><?php echo $idxm; ?></p></td>
                                        </tr>
                                        <tr class="bas ca2">
                                            <td width="50%"><p>TTC</p></td>
                                            <td width="50%"><p>HT</p></td>
                                        </tr>
                                        <tr class="bas tv2">
                                            <td width="50%"><p>Trans°</p></td>
                                            <td width="50%"><p>Nb Art</p></td>
                                        </tr>
                                        <tr class="bas ec2">
                                            <td width="100%" colspan="2"><p>Ecarts caisse</p></td>
                                        </tr>
                                        <tr class="bas popr2">
                                            <td width="100%" colspan="2"><p>Repas</p></td>
                                        </tr>
                                    </table>
                                </th>
                                <?php $z=1; foreach ($month as $idxw => $week) { ?>
                                <th class="width175">
                                    <table width="100%">
                                        <tr class="haut">
                                            <td colspan="2"><p>Semaine <?php echo $z; ?>
                                                    <span id="s<?php echo $idxw; ?>" class="toogle plus" onclick="toggleSemaine(this);"></span></p></td>
                                        </tr>
                                        <tr class="bas ca2">
                                            <td width="50%"><p>TTC</p></td>
                                            <td width="50%"><p>HT</p></td>
                                        </tr>
                                        <tr class="bas tv2">
                                            <td width="50%"><p>Trans°</p></td>
                                            <td width="50%"><p>Nb Art</p></td>
                                        </tr>
                                        <tr class="bas ec2">
                                            <td width="100%" colspan="2"><p>Ecarts caisse</p></td>
                                        </tr>
                                        <tr class="bas popr2">
                                            <td width="100%" colspan="2"><p>Repas</p></td>
                                        </tr>
                                    </table>
                                </th>
                                
                                <?php foreach ($week as $idxd => $day) { ?>
                                <th class="width175 hide s<?php echo $idxw; ?>">
                                    <table class="width175">
                                        <tr class="haut">
                                            <td colspan="2"><p class="blackcolor"><?php echo $idxd.'('.$day->format('d/m/Y').')'; ?></p></td>
                                        </tr>
                                        <tr class="bas ca2">
                                            <td width="50%"><p>TTC</p></td>
                                            <td width="50%"><p>HT</p></td>
                                        </tr>
                                        <tr class="bas tv2">
                                            <td width="50%"><p>Trans°</p></td>
                                            <td width="50%"><p>Nb Art</p></td>
                                        </tr>
                                        <tr class="bas ec2">
                                            <td width="100%" colspan="2"><p>Ecarts caisse</p></td>
                                        </tr>
                                        <tr class="bas popr2">
                                            <td width="100%" colspan="2"><p>Repas</p></td>
                                        </tr>
                                    </table>
                                </th>
                                <?php } ?>
                                
                                <?php $z++; } ?>
                            </tr>
                        </table>
                    </td>
                </tr>
                <tr>
                    <td class="tabs-details-left"><p><?php echo $store->name; ?></p></td>
                    <td class="tabs-details-right">
                        <table>
                            <tr>
                                <td class="width175">
                                    <table class="width175">
                                        <tr class="detailValue ca2">
                                            <td width="50%"><p><?php echo number_format($monthCATTC,2,',', ' '); ?></p></td>
                                            <td width="50%"><p><?php echo number_format($monthCAHT,2,',', ' '); ?></p></td>
                                        </tr>
                                        <tr class="detailValue tv2">
                                            <td width="50%"><p><?php echo number_format($monthNbTran,2,',', ' '); ?></p></td>
                                            <td width="50%"><p><?php echo number_format($monthNbArt,2,',', ' '); ?></p></td>
                                        </tr>
                                        <tr class="detailValue ec2">
                                            <td width="100%" colspan="2"><p><?php echo number_format($monthEc,2,',', ' '); ?></p></td>
                                        </tr>
                                        <tr class="detailValue popr2">
                                            <td width="100%" colspan="2"><p><?php echo number_format($monthRepas,0,',', ' '); ?></p></td>
                                        </tr>
                                    </table>
                                </td>
                                <?php $z=1; foreach ($month as $idxw => $week) { 
                                    // Get CA per week
                                    $weekCATTC = $weekCAHT = 0;                                    
                                    foreach($ca as $dca) {
                                        // Get num of week of orderDate
                                        $orderDate = new \DateTime($dca['orderDate']);            
                                        $orderDateWeek = $orderDate->format('W');
                                        
                                        // Get month of orderDate
                                        $explodeOrderDate = explode('-',$dca['orderDate']);
                                        $thisMonth = $monthText[intval($explodeOrderDate[1])]." ".$explodeOrderDate[0];
                                        
                                        if($orderDateWeek == $idxw && $thisMonth == $idxm) {
                                            $weekCATTC += $dca['ca_ttc'];
                                            $weekCAHT += $dca['ca_ht'];
                                        }
                                    }
                                    
                                    // Get Transaction per week
                                    $weekNbTran = 0;                                    
                                    foreach($nbT as $nbTran) {
                                        // Get num of week of orderDate
                                        $orderDate = new \DateTime($nbTran['orderDate']);            
                                        $orderDateWeek = $orderDate->format('W');
                                        
                                        // Get month of orderDate
                                        $explodeOrderDate = explode('-',$nbTran['orderDate']);
                                        $thisMonth = $monthText[intval($explodeOrderDate[1])]." ".$explodeOrderDate[0];
                                        
                                        if($orderDateWeek == $idxw && $thisMonth == $idxm) {
                                            $weekNbTran += $nbTran['nbTransaction'];
                                        }
                                    }
                                    
                                    // Get Number of Articles per week
                                    $weekNbArt = 0;                                    
                                    foreach($nbA as $nbArt) {
                                        // Get num of week of orderDate
                                        $orderDate = new \DateTime($nbArt['orderDate']);            
                                        $orderDateWeek = $orderDate->format('W');
                                        
                                        // Get month of orderDate
                                        $explodeOrderDate = explode('-',$nbArt['orderDate']);
                                        $thisMonth = $monthText[intval($explodeOrderDate[1])]." ".$explodeOrderDate[0];
                                        
                                        if($orderDateWeek == $idxw && $thisMonth == $idxm) {
                                            $weekNbArt += $nbArt['nbArticle'];
                                        }
                                    }
                                    
                                    // Get Ecarts caisse per week
                                    $weekEc = 0;                                    
                                    foreach($ecDays as $ec) {
                                        // Get num of week of orderDate
                                        $orderDate = new \DateTime($ec['date']);            
                                        $orderDateWeek = $orderDate->format('W');
                                        
                                        // Get month of orderDate
                                        $explodeOrderDate = explode('-',$ec['date']);
                                        $thisMonth = $monthText[intval($explodeOrderDate[1])]." ".$explodeOrderDate[0];
                                        
                                        if($orderDateWeek == $idxw && $thisMonth == $idxm) {
                                            $weekEc += $ec['ec'];
                                        }
                                    }
                                    
                                    // Get Repas per week
                                    $weekRepas = 0;                                    
                                    foreach($repasDays as $repasDay) {
                                        // Get num of week of orderDate
                                        $orderDate = new \DateTime($repasDay['orderDate']);            
                                        $orderDateWeek = $orderDate->format('W');
                                        
                                        // Get month of orderDate
                                        $explodeOrderDate = explode('-',$repasDay['orderDate']);
                                        $thisMonth = $monthText[intval($explodeOrderDate[1])]." ".$explodeOrderDate[0];
                                        
                                        if($orderDateWeek == $idxw && $thisMonth == $idxm) {
                                            $weekRepas += $repasDay['repas'];
                                        }
                                    }
                                ?>
                                <td class="width175">
                                    <table class="width175">
                                        <tr class="detailValue ca2">
                                            <td width="50%"><p><?php echo number_format($weekCATTC,2,',', ' '); ?></p></td>
                                            <td width="50%"><p><?php echo number_format($weekCAHT,2,',', ' '); ?></p></td>
                                        </tr>
                                        <tr class="detailValue tv2">
                                            <td width="50%"><p><?php echo number_format($weekNbTran,0,',', ' '); ?></p></td>
                                            <td width="50%"><p><?php echo number_format($weekNbArt,0,',', ' '); ?></p></td>
                                        </tr>
                                        <tr class="detailValue ec2">
                                            <td width="100%" colspan="2"><p><?php echo number_format($weekEc,2,',', ' '); ?></p></td>
                                        </tr>
                                        <tr class="detailValue popr2">
                                            <td width="100%" colspan="2"><p><?php echo number_format($weekRepas,0,',', ' '); ?></p></td>
                                        </tr>
                                    </table>
                                </td>
                                
                                <?php foreach ($week as $idxd => $day) { 
                                    // Get CA for day
                                    $cattcforday = $cahtforday = 0;
                                    foreach ($ca as $caforday) {
                                        if($caforday['orderDate'] == $day->format('Y-m-d')) {
                                            $cattcforday = $caforday['ca_ttc'];
                                            $cahtforday = $caforday['ca_ht'];
                                        }
                                    }
                                    
                                    // Get Transaction for day
                                    $nbtforday = 0;
                                    foreach ($nbT as $transforday) {
                                        if($transforday['orderDate'] == $day->format('Y-m-d')) {
                                            $nbtforday = $transforday['nbTransaction'];
                                        }
                                    }
                                    
                                    // Get Article for day
                                    $nbaforday = 0;
                                    foreach ($nbA as $artforday) {
                                        if($artforday['orderDate'] == $day->format('Y-m-d')) {
                                            $nbaforday = $artforday['nbArticle'];
                                        }
                                    }
                                    
                                    // Get Ecarts caisse for day
                                    $ecforday = 0;
                                    foreach ($ecDays as $ecday) {
                                        if($ecday['date'] == $day->format('Y-m-d')) {
                                            $ecforday = $ecday['ec'];
                                        }
                                    }
                                    
                                    // Get Repas for day
                                    $repasforday = 0;
                                    foreach ($repasDays as $repasDay) {
                                        if($repasDay['orderDate'] == $day->format('Y-m-d')) {
                                            $repasforday = $repasDay['repas'];
                                        }
                                    }
                                ?>
                                <td class="width175 hide s<?php echo $idxw; ?>">
                                    <table class="width175">
                                        <tr class="detailValue ca2">
                                            <td width="50%"><p><?php echo number_format($cattcforday,2,',', ' '); ?></p></td>
                                            <td width="50%"><p><?php echo number_format($cahtforday,2,',', ' '); ?></p></td>
                                        </tr>
                                        <tr class="detailValue tv2">
                                            <td width="50%"><p><?php echo number_format($nbtforday,0,',', ' '); ?></p></td>
                                            <td width="50%"><p><?php echo number_format($nbaforday,0,',', ' '); ?></p></td>
                                        </tr>
                                        <tr class="detailValue ec2">
                                            <td width="100%"><p><?php echo number_format($ecforday,2,',', ' '); ?></p></td>
                                        </tr>
                                        <tr class="detailValue popr2">
                                            <td width="100%"><p><?php echo number_format($repasforday,0,',', ' '); ?></p></td>
                                        </tr>
                                    </table>
                                </td>
                                <?php } ?>
                                
                                <?php $z++; } ?>
                            </tr>
                        </table>
                    </td>
                </tr>
            </table>
        </div>
    </div>
    <?php $y++; } ?>
</div>